package cn.abcsys.cloud.devops.pipeline.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import cn.abcsys.cloud.devops.pipeline.common.Constant;
import cn.abcsys.cloud.devops.pipeline.common.JenkinsItem;
import cn.abcsys.cloud.devops.pipeline.common.SystemConfig;
import cn.abcsys.cloud.devops.pipeline.core.ImageCore;
import cn.abcsys.cloud.devops.pipeline.core.PipelineCore;
import cn.abcsys.cloud.devops.pipeline.dao.TaskEventMapper;
import cn.abcsys.cloud.devops.pipeline.dao.TaskMapper;
import cn.abcsys.cloud.devops.pipeline.entity.Task;
import cn.abcsys.cloud.devops.pipeline.entity.TaskEvent;
import cn.abcsys.cloud.devops.pipeline.service.TaskEventService;
import cn.abcsys.cloud.devops.pipeline.util.Result;
import net.sf.json.JSONArray;
@Component
public class TaskEventServiceImpl implements TaskEventService {
	private static final Logger LOGGER = Logger.getLogger(TaskEventServiceImpl.class);
	@Autowired
	private TaskEventMapper taskEventMapper;
	@Autowired
	private TaskMapper taskMapper;
	@Autowired
	private PipelineCore pipelineCore;
	@Autowired
	private SystemConfig systemConfig;
	@Autowired
	private ImageCore imageCore;
	@Override
	public Result getTaskEvents(Integer taskId) {
		try {
			List<TaskEvent> taskEvents = taskEventMapper.selectEventsByTaskId(taskId);
			if(taskEvents.isEmpty()){
				return new Result(true, null, "success");
			}else{
				return new Result(true, JSONArray.fromObject(taskEvents), "success");
			}
		} catch (SQLException e) {
			LOGGER.error("Query events by taskId fail:"+e.getMessage());
			return new Result(false, "查询任务事件失败："+e.getMessage());
		}
	}
	@Override
	public Result getTaskEventDetail(Integer eventId) {
		try {
			TaskEvent taskEvent = taskEventMapper.selectEventsByEventId(eventId);
			if(null==taskEvent){
				return new Result(false, "未查到相关事件信息");
			}else{
				return new Result(true, JSONObject.toJSON(taskEvent), "success");
			}
		} catch (SQLException e) {
			LOGGER.error("Query events by taskId fail:"+e.getMessage());
			return new Result(false, "查询任务事件失败："+e.getMessage());
		}
	}
	@Override
	public Result eventLogOutput(Integer eventId) {
		try {
			TaskEvent taskEvent = taskEventMapper.selectEventsByEventId(eventId);
			if(null==taskEvent){
				return new Result(false, "未查到相关事件信息");
			}else{
				//如果是jenkins构建日志，从jenkins api获取
				if(taskEvent.getEventStep()==Constant.JENKINS_STEP){
					Task task = taskMapper.getTaskDetail(taskEvent.getTaskId());
					JenkinsItem jenkinsItem = new JenkinsItem();
					jenkinsItem.setJenkins_user(task.getJenkinsUser());
					jenkinsItem.setJenkins_pwd(task.getJenkinsPwd());
					jenkinsItem.setJenkins_url(task.getJenkinsUrl());
					jenkinsItem.setItem_name(task.getTaskName());
					jenkinsItem.setNumber(taskEvent.getExcuteTimes());
					return pipelineCore.getJobConsoleLog(jenkinsItem);
				}
				//如果是镜像安全扫描日志，从Ci服务器读取
//				if(taskEvent.getEventStep()==Constant.CLAIR_STEP){
//					JSONObject paramIn = new JSONObject();
//					paramIn.put("pipelineHostIp", systemConfig.getPipelineHostIp());
//					paramIn.put("pipelineHostUser", systemConfig.getPipelineHostUser());
//					paramIn.put("pipelineHostPwd", systemConfig.getPipelineHostPwd());
//					paramIn.put("imageClairLogsPath", systemConfig.getImageClairLogsPath());
////					paramIn.put("imageUuid",taskEvent.getMessageInfo());
//					return imageCore.showImageScanResult(paramIn);
//				}
				return new Result(true, taskEvent.getMessageInfo(), "success");
			}
		} catch (SQLException e) {
			LOGGER.error("Query events by taskId fail:"+e.getMessage());
			return new Result(false, "查询任务事件失败："+e.getMessage());
		}
	}
}
